import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress"; // 引入nprogress插件
import "nprogress/nprogress.css"; // 这个nprogress样式必须引入
// import HomeView from '../views/HomeView.vue'
import { login } from "@/api/dict.js";

// 重写push方法
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};
// q:解释一下上面的代码
// a:这是为了解决vue-router在3.0版本以上重复点击路由报错的问题
// q:为什么会报错
// a:因为vue-router在3.0版本以上使用了promise的方式处理路由跳转,如果不处理,重复点击路由会报错
// q:为什么要用call
// a:因为vue-router的源码中并没有直接调用push方法,而是调用了一个名为transitionTo的方法,所以我们要用call来改变this指向
// q:为什么要用catch
// a:因为promise的catch方法可以捕获错误,如果不用catch,重复点击路由会在控制台报错,但是代码不会报错
// q:为什么要return
// a:因为vue-router的源码中的transitionTo方法返回了一个promise
// q:为什么要return routerPush.call
// a:因为routerPush.call返回的是一个promise,如果不return,routerPush方法的返回值就是undefined,这样的话,重复点击路由就会报错  重写replace方法

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    title: "首页",
    component: () => import("../views/home/homePage"),
    redirect: "/UsageAnalysis",
    children: [
      // {
      //   path: '/collection',
      //   name: 'collection',
      //   title: '汇总',
      //   icon: '@/assets/layout/collect.png',
      //   active_icon: '@/assets/layout/collect_active.png',
      //   component: () => import('../views/collection/index'),
      // },
      // {
      //   path: '/photovoltaic',
      //   name: 'photovoltaic',
      //   title: '光伏',
      //   component: () => import('../views/photovoltaic/index'),
      // },
      {
        bigTitle: "储能需求分析",
        children: [
          {
            path: "/UsageAnalysis",
            name: "UsageAnalysis",
            title: "储能使用率分析",
            meta: {
              title: "储能使用率分析",
            },
            component: () => import("../views/UsageAnalysis/index.vue"),
          },
          {
            path: "/UsageCount",
            name: "UsageCount",
            title: "源荷平衡统计",
            meta: {
              title: "源荷平衡统计",
            },
            component: () => import("../views/UsageCount/index.vue"),
          },
          {
            path: "/maintenanceRecord",
            name: "maintenanceRecord",
            title: "电网状态留存",
            meta: {
              title: "电网状态留存",
            },
            component: () => import("../views/maintenanceRecord/index.vue"),
          },
        ],
        path: "/maintenanceRecord",
        component: () => import("../views/home1/homePage1"),
      },

      {
        bigTitle: "储能优化配置",
        children: [
          {
            path: "/OrderSetting",
            name: "OrderSetting",
            title: "优化目标设定",
            meta: {
              title: "优化目标设定",
            },
            component: () => import("../views/OrderSetting/index.vue"),
          },
          {
            path: "/sycngh",
            name: "sycngh",
            title: "时域储能规划",
            meta: {
              title: "时域储能规划",
            },
            component: () => import("../views/sycngh/index.vue"),
          },
          {
            path: "/cnrlAnalysis",
            name: "cnrlAnalysis",
            title: "储能容量分析",
            meta: {
              title: "储能容量分析",
            },
            component: () => import("../views/cnrlAnalysis/index.vue"),
          },
          {
            path: "/cnghRecommend",
            name: "cnghRecommend",
            title: "储能规划推荐",
            meta: {
              title: "储能规划推荐",
            },
            component: () => import("../views/cnghRecommend/index.vue"),
          },
        ],
        path: "/maintenanceRecord",
        component: () => import("../views/home1/homePage1"),
      },

      {
        bigTitle: "配置结果展示",
        children: [
          {
            path: "/ConfigurationRevision",
            name: "ConfigurationRevision",
            title: "配置方案修正",
            meta: {
              title: "配置方案修正",
            },
            component: () => import("../views/ConfigurationRevision/index.vue"),
          },
          {
            path: "/ConfigurationCheck",
            name: "ConfigurationCheck",
            title: "配置方案校核",
            meta: {
              title: "配置方案校核",
            },
            component: () => import("../views/ConfigurationCheck/index.vue"),
          },
          {
            path: "/ConfigurationShow",
            name: "ConfigurationShow",
            title: "配置方案展示",
            meta: {
              title: "配置方案展示",
            },
            component: () => import("../views/ConfigurationShow/index.vue"),
          },
          {
            path: "/ConfigurationAnalysis",
            name: "ConfigurationAnalysis",
            title: "预期效果分析",
            meta: {
              title: "预期效果分析",
            },
            component: () => import("../views/ConfigurationAnalysis/index.vue"),
          },
        ],
        path: "/maintenanceRecord",
        component: () => import("../views/home1/homePage1"),
      },
      // {
      //   bigTitle: "台账管理",
      //   children: [
      //     {
      //       path: "/ledger",
      //       name: "ledger",
      //       title: "频率响应台账",
      //       component: () => import("../views/windPower/windpower.vue"),
      //     },
      //     {
      //       path: "/maintenanceRecord",
      //       name: "maintenanceRecord",
      //       title: "维护记录",
      //       component: () => import("../views/maintenanceRecord/index"),
      //     },
      //   ],
      //   path: "/maintenanceRecord",
      //   component: () => import("../views/home1/homePage1"),
      // },
      // {
      //   bigTitle: "试验管理",
      //   children: [
      //     {
      //       path: "/testResult",
      //       name: "testResult",
      //       title: "试验方案",
      //       component: () => import("../views/testResult/testResult.vue"),
      //     },
      //     {
      //       path: "/testResultQuery",
      //       name: "testResultQuery",
      //       title: "试验结果",
      //       component: () =>
      //         import("../views/testResultQuery/testResultQuery.vue"),
      //     },
      //   ],
      //   path: "/testResult",
      //   component: () => import("../views/home1/homePage1"),
      // },
      // {
      //   bigTitle: "定值参数管理",
      //   children: [
      //     {
      //       path: "/customOrderManagement",
      //       name: "customOrderManagement",
      //       title: "定值单管理",
      //       component: () => import("../views/customOrderManagement/index.vue"),
      //     },
      //   ],
      //   path: "/customOrderManagement",
      //   component: () => import("../views/home1/homePage1"),
      // },
      // {
      //   bigTitle: "在线监测",
      //   children: [
      //     {
      //       path: "/onlineMonitor",
      //       name: "onlineMonitor",
      //       title: "在线监测",
      //       component: () => import("../views/onlineMonitor/onlineMonitor.vue"),
      //     },
      //     {
      //       path: "/rawData",
      //       name: "rawData",
      //       title: "原始数据",
      //       component: () => import("../views/rawData/rawData.vue"),
      //     },
      //   ],
      //   path: "/onlineMonitor",
      //   component: () => import("../views/home1/homePage1"),
      // },
      // {
      //   bigTitle: '主动测试',
      //   children: [
      //     // {
      //     //   path: '/onlineMonitor',
      //     //   name: 'onlineMonitor',
      //     //   title: '案例准备',
      //     //   component: () => import('../views/onlineMonitor/onlineMonitor.vue'),
      //     // },
      //     // {
      //     //   path: '/rawData',
      //     //   name: 'rawData',
      //     //   title: '测试设置',
      //     //   component: () => import('../views/rawData/rawData.vue'),
      //     // },
      //     // {
      //     //   path: '/rawData',
      //     //   name: 'rawData',
      //     //   title: '过程监测',
      //     //   component: () => import('../views/rawData/rawData.vue'),
      //     // },
      //     // {
      //     //   path: '/rawData',
      //     //   name: 'rawData',
      //     //   title: '指令查询',
      //     //   component: () => import('../views/rawData/rawData.vue'),
      //     // },
      //     {
      //       path: '/evaluationAnalysis',
      //       name: 'evaluationAnalysis',
      //       title: '评价分析',
      //       component: () => import('../views/evaluationAnalysis/evaluationAnalysis.vue'),
      //     },
      //     {
      //       path: '/teatRawData',
      //       name: 'teatRawData',
      //       title: '原始数据',
      //       component: () => import('../views/testRawData/rawData.vue'),
      //     },
      //   ],
      //   path: '/onlineMonitor',
      //   component: () => import('../views/home1/homePage1'),
      // },
    ],
  },
];

// const routes = [
//   {
//         path: '/',
//         name: 'home',
//         title: '首页',
//         component: () => import('../views/all/index'),
//         redirect: '/index'
//       },
//   {
//         path: '/index',
//         name: 'index',
//         title: '源往荷储总览图',
//         component: () => import('../views/all/index'),
//   },
//   {
//     path: '/sjcdwl',
//     name: 'sjcdwl',
//     title: '省级充电网络',
//     component: () => import('../views/sjcdwl/index'),
// },
// {
//   path: '/test',
//   name: 'test',
//   title: '测试',
//   component: () => import('../views/test/index'),
// },
// ]

//获取url携带的参数
function getUrlParams() {
  //获取url的参数
  let url = window.location.href;
  // 通过 ? 分割获取后面的参数字符串
  let urlStr = url.split("?")[1];
  // 创建空对象存储参数
  let obj = {};
  if (urlStr) {
    // 再通过 & 将每一个参数单独分割出来
    let paramsArr = urlStr.split("&");
    for (let i = 0, len = paramsArr.length; i < len; i++) {
      // 再通过 = 将每一个参数分割为 key:value 的形式
      let arr = paramsArr[i].split("=");
      obj[arr[0]] = arr[1];
    }
  }
  return obj;
}

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  // 加载开始
  NProgress.start();
  next();

  document.title = to.meta.title || "智慧配电网仿真平台";
  // 首次进入，直接跳到登录界面
  let urlTicket = getUrlParams();
  // let ticket = null
  //一进来先根据localStorage有没有ticket和url里有没有ticket,没有就跳转到登录页面
  let CtrToken = window.localStorage.getItem("Ctr-Token");
  let baseURL = process.env.VUE_APP_BASE_API;
  // 低代码平台
  // if (!urlTicket.accessToken) {
  //   // oms
  //   if (CtrToken == null || CtrToken == undefined || CtrToken == '') {
  //     if (urlTicket.ticket == null || urlTicket.ticket == undefined || urlTicket.ticket == '') {
  //       // window.location.href = baseURL + `/cas/login?service=` + baseURL + `/yctp/`
  //       next(window.location.href = baseURL + `/cas/login?service=` + baseURL + `/yctp/`)
  //       return false
  //     } else {
  //       // window.localStorage.setItem('ticket', urlTicket.ticket)
  //       login({ticket:urlTicket.ticket}).then((res) => {
  //         if (res.code === '000000') {
  //           window.localStorage.setItem('Ctr-Token',res.data)
  //           next()
  //         }
  //       })
  //     }
  //   }else{
  //     next()
  //   }
  // } else {
  //   window.localStorage.setItem('accessToken', urlTicket.accessToken)
  //   next()
  // }
});

router.afterEach((to, from, next) => {
  // 加载结束
  // localStorage.setItem('loading', false)
  NProgress.done();
  // next()
});
export default router;
